package com.vanysoft.business.data.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.vanysoft.business.data.entity.PlantUser;
import com.vanysoft.business.data.service.PlantUserService;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.framework.aop.VanySoftValidation;
import com.vanysoft.framework.aop.VanySoftValidationException;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.nio.file.Watchable;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 工厂人员信息管理 前端控制器
 * </p>
 *
 * @author YangLei
 * @since 2018-07-01
 */
@Controller
@RequestMapping("/data/plantUser")
public class PlantUserController extends BaseController {

    @Autowired
    PlantUserService plantUserService;

    /**
     * 保存工厂人员信息
     *
     * @param plantUser
     * @param result
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    @VanySoftValidation
    public JSONResponse save(@Valid PlantUser plantUser, BindingResult result, @SessionUser UserInfo userInfo) {
        PlantUser entity = plantUserService.selectOne(plantUser);
        if (entity != null) {
            throw new VanySoftValidationException(getMessage("common.hint.exist"));
        }
        plantUser.setCreateTime(new Date());
        String userId = userInfo.getId();
        plantUser.setCreateUser(userId);
        plantUser.setModifyTime(new Date());
        plantUser.setModifyUser(userId);
        plantUserService.insert(plantUser);
        return new JSONResponse();
    }

    /**
     * 更新工厂人员信息
     *
     * @param plantUser
     * @param result
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    @VanySoftValidation
    public JSONResponse update(@Valid PlantUser plantUser, BindingResult result, @SessionUser UserInfo userInfo) {
        PlantUser entity = plantUserService.selectOne(plantUser);
        if (entity != null) {
            throw new VanySoftValidationException(getMessage("common.hint.exist"));
        }
        plantUser.setModifyTime(new Date());
        plantUser.setModifyUser(userInfo.getId());
        plantUserService.updateById(plantUser);
        PlantUser user = plantUserService.selectById(plantUser.getId());
        //PPM发生变更
        if (!user.getProcurementManager().equals(plantUser.getProcurementManager())) {
            PlantUser pu = new PlantUser();
            pu.setProcurementManager(plantUser.getProcurementManager());
            pu.setModifyTime(new Date());
            pu.setModifyUser(userInfo.getId());
            Wrapper<PlantUser> wrapper = new EntityWrapper<PlantUser>().eq("is_enable", 1).eq("plant_code", plantUser.getPlantCode());
            plantUserService.update(pu, wrapper);
        }
        return new JSONResponse();
    }

    /**
     * 分页显示工厂人员
     *
     * @param page
     * @param plant
     * @param user
     * @param status
     * @param request
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public JSONResponse list(Page<PlantUser> page, String plant, String user, int status, HttpServletRequest request) {
        Locale locale = request.getLocale();
        page = plantUserService.selectPage(page, plant, user, status, locale.getLanguage());
        return new JSONResponse(page);
    }

    /**
     * 删除工厂人员
     *
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public JSONResponse delete(String id) {
        PlantUser plantUser = plantUserService.selectById(id);
        Wrapper<PlantUser> wrapper = new EntityWrapper<PlantUser>().eq("is_enable", 1).eq("plant_code", plantUser.getPlantCode());
        List<PlantUser> list = plantUserService.selectList(wrapper);
        if (list.size() > 1) {
            plantUser.setIsEnable("0");
            plantUserService.updateById(plantUser);
        }else{
            throw new VanySoftValidationException(getMessage("plant.assignment.hint.delete"));
        }
        return new JSONResponse();
    }


    /**
     * 根据工厂code查询单条信息
     *
     * @param code
     * @return
     */
    @RequestMapping("one")
    @ResponseBody
    public JSONResponse query(String code) {
        PlantUser plantUser = plantUserService.selectOne(new EntityWrapper<PlantUser>().eq("plant_code", code).eq("is_enable", "1"));
        return new JSONResponse(plantUser);
    }
}

